package GUI;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Simulator.Bird;
import Simulator.Fox;
import Simulator.Rabbit;

@SuppressWarnings("serial")
public class SettingsPane extends JFrame {
	private JLabel heightLabel, widthLabel;
	private JButton ok, close, defSettings;
	private JSlider height, width, maxAge, breedingAge, probability, litter;
	int defBreedAge = 0;
	int defMaxAge = 0;
	int defProbability = 0;
	int defLitter = 0;
	Container contentPane;

	public SettingsPane() {
		this.setLocationByPlatform(true);
		this.setAlwaysOnTop(true);
		contentPane = this.getContentPane();
		contentPane.setLayout(new GridLayout(0, 1, 2, 10));
	}

	public void setRabbitsPane() {
		this.setTitle("Rabbit settings");
		this.setVisible(true);
		setAnimalSliders("rabbit");
		JPanel buttons = new JPanel();
		defSettings = new JButton("Default");
		defSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == defSettings) {
					restoreDefaults("rabbit");
				}
			}
		});
		ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == ok) {
					double prob = probability.getValue();
					prob = prob / 100;
					Rabbit.setUp(maxAge.getValue(), breedingAge.getValue(),
							prob, litter.getValue());
				}
			}
		});
		close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == close) {
					setVisible(false);
				}
			}
		});
		buttons.add(defSettings);
		buttons.add(ok);
		buttons.add(close);
		contentPane.add(buttons);
	}

	public void setFoxPane() {
		this.setTitle("Fox settings");
		this.setVisible(true);
		setAnimalSliders("fox");
		JPanel buttons = new JPanel();
		defSettings = new JButton("Default");
		defSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == defSettings) {
					restoreDefaults("fox");
				}
			}
		});
		ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == ok) {
					double prob = probability.getValue();
					prob = prob / 100;
					Fox.setUp(maxAge.getValue(), breedingAge.getValue(), prob,
							litter.getValue());
				}
			}
		});
		close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == close) {
					setVisible(false);
				}
			}
		});
		buttons.add(defSettings);
		buttons.add(ok);
		buttons.add(close);
		contentPane.add(buttons);
	}

	public void setBirdPane() {
		this.setTitle("Bird settings");
		this.setVisible(true);
		setAnimalSliders("bird");
		JPanel buttons = new JPanel();
		defSettings = new JButton("Default");
		defSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == defSettings) {
					restoreDefaults("bird");
				}
			}
		});
		ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == ok) {
					double prob = probability.getValue();
					prob = prob / 100;
					Bird.setUp(maxAge.getValue(), breedingAge.getValue(), prob,
							litter.getValue());
				}
			}
		});
		close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == close) {
					setVisible(false);
				}
			}
		});
		buttons.add(defSettings);
		buttons.add(ok);
		buttons.add(close);
		contentPane.add(buttons);
	}

	public void setOptionsPane() {
		this.setTitle("Settings");
		this.setPreferredSize(new Dimension(250, 500));
		this.pack();
		this.setVisible(true);
		Hashtable<Integer, JLabel> heightTable = new Hashtable<Integer, JLabel>();
		heightTable.put(0, new JLabel("0"));
		heightTable.put(100, new JLabel("100"));
		heightTable.put(200, new JLabel("200"));
		Hashtable<Integer, JLabel> widthTable = new Hashtable<Integer, JLabel>();
		widthTable.put(0, new JLabel("0"));
		widthTable.put(100, new JLabel("100"));
		widthTable.put(200, new JLabel("200"));
		height = new JSlider(0, 200, 50);
		heightLabel = new JLabel("Height: 50");
		height.setBorder(new TitledBorder("Height of the simulator"));
		height.setLabelTable(heightTable);
		height.setPaintLabels(true);
		height.setToolTipText("Height");
		height.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				heightLabel.setText("Height: " + height.getValue());
			}
		});
		width = new JSlider(0, 200, 50);
		widthLabel = new JLabel("Steps: 50");
		width.setBorder(new TitledBorder("Height of the simulator"));
		width.setLabelTable(heightTable);
		width.setPaintLabels(true);
		width.setToolTipText("Height");
		width.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				widthLabel.setText("Steps: " + width.getValue());
			}
		});
		JPanel buttons = new JPanel();
		defSettings = new JButton("Default");
		defSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == defSettings) {
					height.setValue(50);
					width.setValue(50);
				}
			}
		});
		ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == ok) {
					GUI.getSimulator().setSize(height.getValue(),
							width.getValue());
					GUI.getSimulator().reset();
					setVisible(false);
				}
			}
		});
		close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == close) {
					setVisible(false);
				}
			}
		});
		buttons.add(defSettings);
		buttons.add(ok);
		buttons.add(close);
		contentPane.add(height);
		contentPane.add(heightLabel);
		contentPane.add(width);
		contentPane.add(widthLabel);
		contentPane.add(buttons);
	}

	public void setAnimalSliders(String animal) {
		setDefaults(animal);

		Hashtable<Integer, JLabel> maxAgeTable = new Hashtable<Integer, JLabel>();
		maxAgeTable.put(0, new JLabel("0"));
		maxAgeTable.put(100, new JLabel("100"));
		maxAgeTable.put(200, new JLabel("200"));

		Hashtable<Integer, JLabel> breedingAgeTable = new Hashtable<Integer, JLabel>();
		breedingAgeTable.put(0, new JLabel("0"));
		breedingAgeTable.put(25, new JLabel("25"));
		breedingAgeTable.put(50, new JLabel("50"));

		Hashtable<Integer, JLabel> probabilityTable = new Hashtable<Integer, JLabel>();
		probabilityTable.put(0, new JLabel("0"));
		probabilityTable.put(50, new JLabel("50"));
		probabilityTable.put(100, new JLabel("100"));

		Hashtable<Integer, JLabel> litterTable = new Hashtable<Integer, JLabel>();
		litterTable.put(0, new JLabel("0"));
		litterTable.put(5, new JLabel("5"));
		litterTable.put(10, new JLabel("10"));

		maxAge = new JSlider(0, 200, defMaxAge);
		maxAge.setBorder(new TitledBorder("Maximum Age"));
		maxAge.setLabelTable(maxAgeTable);
		maxAge.setPaintLabels(true);
		maxAge.setToolTipText("Max Age");

		breedingAge = new JSlider(0, 50, defBreedAge);
		breedingAge.setBorder(new TitledBorder("Breeding Age"));
		breedingAge.setLabelTable(breedingAgeTable);
		breedingAge.setPaintLabels(true);
		breedingAge.setToolTipText("Breeding Age");

		probability = new JSlider(0, 100, defProbability);
		probability.setBorder(new TitledBorder("Breeding Probability"));
		probability.setLabelTable(probabilityTable);
		probability.setPaintLabels(true);
		probability.setToolTipText("Breeding Probability");

		litter = new JSlider(0, 10, defLitter);
		litter.setBorder(new TitledBorder("Number of ofspring"));
		litter.setLabelTable(litterTable);
		litter.setPaintLabels(true);
		litter.setToolTipText("Number of ofspring");

		contentPane.add(maxAge);
		contentPane.add(breedingAge);
		contentPane.add(probability);
		contentPane.add(litter);

		this.setPreferredSize(new Dimension(350, 550));
		this.pack();
	}

	public void setDefaults(String animal) {
		String breed = animal;
		if (breed.equals("rabbit")) {
			defBreedAge = 5;
			defMaxAge = 50;
			defProbability = 15;
			defLitter = 3;
		} else if (breed.equals("fox")) {
			defBreedAge = 10;
			defMaxAge = 150;
			defProbability = 20;
			defLitter = 5;
		} else if (breed.equals("bird")) {
			defBreedAge = 3;
			defMaxAge = 30;
			defProbability = 12;
			defLitter = 3;
		}
	}

	public void restoreDefaults(String animal) {
		String breed = animal;
		if (breed.equals("rabbit")) {
			defBreedAge = 5;
			breedingAge.setValue(defBreedAge);
			defMaxAge = 50;
			maxAge.setValue(defMaxAge);
			defProbability = 15;
			probability.setValue(defProbability);
			defLitter = 3;
			litter.setValue(defLitter);
			Rabbit.setUp(defMaxAge, defBreedAge, defProbability, defLitter);
		} else if (breed.equals("fox")) {
			defBreedAge = 10;
			breedingAge.setValue(defBreedAge);
			defMaxAge = 150;
			maxAge.setValue(defMaxAge);
			defProbability = 20;
			probability.setValue(defProbability);
			defLitter = 5;
			litter.setValue(defLitter);
			Fox.setUp(defMaxAge, defBreedAge, defProbability, defLitter);
		} else if (breed.equals("bird")) {
			defBreedAge = 3;
			breedingAge.setValue(defBreedAge);
			defMaxAge = 30;
			maxAge.setValue(defMaxAge);
			defProbability = 12;
			probability.setValue(defProbability);
			defLitter = 3;
			litter.setValue(defLitter);
			Bird.setUp(defMaxAge, defBreedAge, defProbability, defLitter);
		}
	}

	public void makeLegend() {
		this.setTitle("Legend");
		this.setLocation(1000, 0);
		this.setPreferredSize(new Dimension(250, 250));
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

		JPanel rabbit, fox, bird, hunter;
		JLabel rabLabel, foxLabel, birdLabel, hunterLabel;

		rabbit = new JPanel();
		rabbit.setBackground(Color.orange);
		rabLabel = new JLabel();
		rabLabel.setText("rabbit");
		rabLabel.setFont(new Font("Arial", Font.BOLD, 24));
		rabLabel.setForeground(Color.white);
		rabbit.add(rabLabel);

		fox = new JPanel();
		fox.setBackground(Color.red);
		foxLabel = new JLabel();
		foxLabel.setText("fox");
		foxLabel.setFont(new Font("Arial", Font.BOLD, 24));
		foxLabel.setForeground(Color.white);
		fox.add(foxLabel);

		bird = new JPanel();
		bird.setBackground(Color.blue);
		birdLabel = new JLabel();
		birdLabel.setText("bird");
		birdLabel.setFont(new Font("Arial", Font.BOLD, 24));
		birdLabel.setForeground(Color.white);
		bird.add(birdLabel);

		hunter = new JPanel();
		hunter.setBackground(Color.green);
		hunterLabel = new JLabel();
		hunterLabel.setText("hunter");
		hunterLabel.setFont(new Font("Arial", Font.BOLD, 24));
		hunterLabel.setForeground(Color.white);
		hunter.add(hunterLabel);

		contentPane.add(rabbit);
		contentPane.add(fox);
		contentPane.add(bird);
		contentPane.add(hunter);

		this.setVisible(true);
		this.pack();
	}
}
